//package com.pkx.guide.task;
//
//import com.pkx.guide.common.constant.Constants;
//import com.pkx.guide.fcms.service.FcmsPlayService;
//import com.pkx.guide.info.entity.TInfoGuide;
//import com.pkx.guide.info.entity.TInfoGuidePublish;
//import com.pkx.guide.info.entity.TInfoNetworkConfig;
//import com.pkx.guide.info.service.TInfoGuidePublishService;
//import com.pkx.guide.info.service.TInfoGuideService;
//import com.pkx.guide.info.service.TInfoNetworkConfigService;
//import com.pkx.guide.qianfeng.service.QianFengPlayLedService;
//import com.pkx.guide.record.entity.TRecordGuidePublishTask;
//import com.pkx.guide.record.service.TRecordGuidePublishTaskService;
//import jakarta.annotation.Resource;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.stereotype.Component;
//
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//import java.util.Timer;
//
///**
// * 程序启动后，初始正在执行的任务
// */
//@Component
//@Slf4j
//public class AutoPublishTimeTaskInit implements CommandLineRunner {
//
//    @Resource
//    private TInfoGuidePublishService guidePublishService;
//
//    @Resource
//    private TRecordGuidePublishTaskService guidePublishTaskService;
//
//    @Resource
//    private FcmsPlayService fcmsPlayService;
//
//    @Resource
//    private TInfoGuideService guideService;
//
//    @Resource
//    private TInfoNetworkConfigService infoNetworkConfigService;
//
//    @Resource
//    private QianFengPlayLedService qianFengPlayLedService;
//
//    @Override
//    public void run(String... args) throws Exception {
//
//        List<TRecordGuidePublishTask> guidePublishTaskList = guidePublishTaskService.selectAll();
//        if (guidePublishTaskList == null || guidePublishTaskList.size() == 0){
//            return;
//        }
//        // 查询乾丰上位机网络配置
//        TInfoNetworkConfig networkConfig = infoNetworkConfigService.selectByGuideType(1);
//        Date currentDate = new Date();
//        for (TRecordGuidePublishTask tRecordGuidePublishTask : guidePublishTaskList) {
//            // 查询发布信息
//            TInfoGuidePublish guidePublish = guidePublishService.selectById(tRecordGuidePublishTask.getGuidePublishId());
//            if (guidePublish == null || guidePublish.getDelFlg() == 1 || guidePublish.getModeType() == 1) {
//                continue;
//            }
//            // 发布时间
//            String publishTimeStr = tRecordGuidePublishTask.getPublishTimeScale();
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//            Calendar calendar = Calendar.getInstance();
//            //获取年
//            int year = calendar.get(Calendar.YEAR);
//            //获取月份，0表示1月份
//            int month = calendar.get(Calendar.MONTH) + 1;
//            //获取当前天数
//            int day = calendar.get(Calendar.DAY_OF_MONTH);
//            Date publishTime = null;
//            try {
//                publishTime =  format.parse( year + "-" + month + "-" + day + " " + publishTimeStr);
//            } catch (ParseException e) {
//                e.printStackTrace();
//                continue;
//            }
//            // 如果发布时间已过，则加一天执行，未过，直接执行
//            if (currentDate.compareTo(publishTime) > 0){
//                calendar.setTime(publishTime);
//                calendar.add(Calendar.DAY_OF_MONTH, 1);
//                publishTime = calendar.getTime();
//            }
//            Timer timer = new Timer();
//            if (guidePublish.getGuideType() != null && guidePublish.getGuideType() == 1){
//                QianFengAutoPublishTimeTask autoPublishTimeTask = new QianFengAutoPublishTimeTask(guidePublishService, qianFengPlayLedService, tRecordGuidePublishTask.getGuidePublishId(), networkConfig, timer);
//                timer.schedule(autoPublishTimeTask, publishTime, tRecordGuidePublishTask.getPeriod());
//            } else {
//                // 查询情报板信息
//                TInfoGuide guide = guideService.selectById(guidePublish.getGuideId());
//                if (guide == null){
//                    continue;
//                }
//                AutoPublishTimeTask autoPublishTimeTask = new AutoPublishTimeTask(guidePublishService, fcmsPlayService, tRecordGuidePublishTask.getGuidePublishId(), guide, timer);
//                timer.schedule(autoPublishTimeTask, publishTime, tRecordGuidePublishTask.getPeriod());
//            }
//            Constants.guidePublishTimerMap.put(tRecordGuidePublishTask.getGuidePublishId(), timer);
//        }
//    }
//}
